package com.pdool.h5red;

import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 种鑫
 * @date 2023/12/19 10:18
 */
@RestController
@Slf4j
public class GameController {
    @Resource
    GameService gameService;


    @GetMapping("/today/{uid}")
    @ApiOperation(value = "查询今天的信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataTypeClass = String.class, paramType = "path"),
    })
    public ResponseEntity<TodayScoreDTO> queryTodayInfo(@PathVariable String uid) {
        TodayScoreDTO maxScore = gameService.queryTodayInfo(uid);
        return ResponseEntity.ok(maxScore);
    }

    @PostMapping("/save/{uid}")
    @ApiOperation(value = "保存游戏数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataTypeClass = String.class, paramType = "path"),
    })
    public ResponseEntity saveScore(@PathVariable String uid, @RequestBody RoundInfoDTO roundInfoDTO) {
//        log.info("request {}", JSONUtil.toJsonStr(roundInfoDTO));
        boolean isOK = gameService.checkGameData(roundInfoDTO);
        if (isOK) {
            JSONObject result = gameService.saveMaxScore(uid, roundInfoDTO);
            TodayScoreDTO maxScore = gameService.queryTodayInfo(uid);
            JSONObject jsonObject = new JSONObject();
            if (result != null) {
                jsonObject.putAll(result);
            }
            jsonObject.put("todayInfo", maxScore);
            return ResponseEntity.ok(jsonObject);
        } else {
            return ResponseEntity.ok("请不要作弊");
        }
    }

    @GetMapping("/open/{uid}")
    @ApiOperation(value = "开始游戏", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataTypeClass = String.class, paramType = "path"),
    })
    public ResponseEntity openGame(@PathVariable String uid) {
        gameService.openGame(uid);
        return ResponseEntity.ok("success");
    }
}
